﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using VDataAdapter.EventManagement;
using VDataAdapter.PriceServerAdapter;

namespace VDataAdapter.QueueManagement
{
    

    class PSEventFromQueueFactory : IPSEventQueueEnqueuerFactory
    {
        private Queue<Event> _queue;
        private int _milliseconds;

        /// <summary>
        /// Construct a PSEventQueueEnqueuer with its source from file
        /// </summary>
        /// <param name="queue">Event Queue to play back</param>
        /// <param name="milliseconds">Speed of playback</param>
        public PSEventFromQueueFactory(Queue<Event> queue, int milliseconds)
        {
            _queue = queue;
            _milliseconds = milliseconds;
        }
        public PSEventQueueEnqueuer createPSEventHandler(IEventStoteredQueue eventQueue, PSListenerExceptionHandler handler)
        {
            return new LocalEventEnqueuer(eventQueue, _queue, handler, _milliseconds);
        }
    }
    /// <summary>
    /// This class dequeue from events queue
    /// and put those events into IEventStoredQueue
    /// </summary>
    class LocalEventEnqueuer : PSEventQueueEnqueuer
    { 
        Queue<Event> _source;
        private bool _stop = false;
        private int _milliseconds;
        /// <summary>
        /// Dequeue from events and enqueue to IEventStoteredQueue
        /// </summary>
        /// <param name="queue"></param>
        public LocalEventEnqueuer(IEventStoteredQueue queue, Queue<Event> events, PSListenerExceptionHandler handler, int milliseconds)
            : base(queue, handler)
        {   
            _source = events;
            _milliseconds = milliseconds;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="milliseconds"></param>
        /// <param name="events"></param>
        private void StartPutEvent(int milliseconds)
        {
            System.Threading.Thread.Sleep(milliseconds);

            while(_source.Count > 0 && _stop != false)
            {
                var e = _source.Dequeue();
                if (e is FirstTimeEvent)
                {
                   _eventQueue.PutFirstTimeEvent((FirstTimeEvent)e);
                }
                if (e is MarketInfosEvent)
                {
                    _eventQueue.PutMarketInfosEvent((MarketInfosEvent)e);
                }
                if (e is Top3PricesEvent)
                {
                    _eventQueue.PutTop3PricesEvent((Top3PricesEvent)e);
                }
                if (e is PtTopPricesChangedEvent)
                {
                    _eventQueue.PutPtTopPricesEvent((PtTopPricesChangedEvent)e);
                }
                if (e is PtTransactionsChangedEvent)
                {
                    _eventQueue.PutPtTransactionsEvent((PtTransactionsChangedEvent)e);
                }
                if(e is TransactionsEvent)
                {
                    _eventQueue.PutTransactionsEvent((TransactionsEvent)e);
                }
            }
        }
 
        public override void StartListening()
        {
            StartPutEvent(_milliseconds);
        }

        public override void StopListening()
        {
            _stop = false;
        }
    }
}
